package et.research;

import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.util.ResourceLocation;
import thaumcraft.api.research.ResearchCategories;
import thaumcraft.api.research.ResearchItem;

import java.lang.reflect.Field;

/**
 * Created by winston_wang on 2017/6/2.
 */
public class ETResearchManager {
    public static final String CATEGORY = "ET";
    public ETResearchManager(String mods[]){
        initResearchCategory();
        initResearchRegistry(mods);
    }
    private void initResearchCategory(){
        ResearchCategories.registerCategory(ETResearchManager.CATEGORY, new ResourceLocation("et", "textures/items/itemVoidDrill.png"), new ResourceLocation("thaumcraft", "textures/gui/gui_researchback.png"));
    }
    private void initResearchRegistry(String mods[]){
        Class researchClass = ETResearchs.class;
        for(Field field : researchClass.getDeclaredFields()){
            if (field.isAnnotationPresent(ETResearch.class)){
                ETResearch etResearch = field.getAnnotation(ETResearch.class);
                if(etResearch.dependMods().length!=0) {
                    if(etResearch.dependMods().length==mods.length) {
                        int index = 0;
                        for (String modid : etResearch.dependMods()) {
                            for(String checkid : mods){
                                if (!modid.contains(checkid)) {
                                    continue;
                                }
                            }
                            index++;
                        }
                        try {
                            ((ResearchItem)field.get(null)).registerResearchItem();
                        }catch (Exception e){
                            e.printStackTrace();
                            FMLCommonHandler.instance().exitJava(-125,true);
                        }
                    }
                }else {
                    try {
                        ((ResearchItem)field.get(null)).registerResearchItem();
                    }catch (Exception e){
                        e.printStackTrace();
                        FMLCommonHandler.instance().exitJava(-125,true);
                    }
                }
            }
        }
    }
}
